<script setup>
import { ref, watch } from 'vue'
import { regUser, loginUser } from '@/api/user'
import { useRouter } from 'vue-router'
import { setUserStore } from '@/stores/modules/user'

defineOptions({
  name: 'AppLoginSign'
})

const isRegisted = ref(false)

const formModel = ref({
  username: '',
  password: '',
  againPassword: '',
  rememberMe: false
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名长度在5到10个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码长度位6-15位的非空字符',
      trigger: 'blur'
    }
  ],
  againPassword: [
    { required: true, message: '两次密码输入不一致', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码长度位6-15位的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== formModel.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const form = ref()
const router = useRouter()
const userStore = setUserStore()
const submit = async () => {
  if (isRegisted.value) {
    await form.value.validate()
    await regUser({
      username: formModel.value.username,
      password: formModel.value.password,
      repassword: formModel.value.againPassword
    })
    ElMessage.success('注册成功')
    isRegisted.value = false
  } else {
    await form.value.validate()
    const token = await loginUser({
      username: formModel.value.username,
      password: formModel.value.password
    })
    userStore.setToken(token.data.token)
    ElMessage.success('登录成功')
    router.push('/layout')
  }
}

watch(isRegisted, () => {
  formModel.value.username = ''
  formModel.value.password = ''
  formModel.value.againPassword = ''
})
</script>

<template>
  <el-row>
    <el-col :span="12" class="bg">
      <img src="@/assets/text.png" alt="" />
    </el-col>
    <el-col
      :span="6"
      :offset="3"
      style="margin-left: 12.5%; display: flex; align-items: center"
    >
      <el-form ref="form" :model="formModel" :rules="rules">
        <h2>
          {{ isRegisted ? '注册' : '登录' }}
        </h2>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            prefix-icon="User"
            placeholder="请输入用户名"
            class=""
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            prefix-icon="Lock"
            class=""
            placeholder="请输入密码"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="againPassword" v-if="isRegisted">
          <el-input
            v-model="formModel.againPassword"
            prefix-icon="Lock"
            class=""
            placeholder="请输入相同的密码"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
        <div style="display: flex; justify-content: space-between" v-else>
          <el-checkbox type="primary">记住密码</el-checkbox>
          <el-link type="primary" :underline="false">忘记密码？</el-link>
        </div>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="submit">{{
            isRegisted ? '注册' : '登录'
          }}</el-button>
        </el-form-item>
        <el-link
          :underline="false"
          class="text-secondary my-5"
          @click="isRegisted = !isRegisted"
          >{{ isRegisted ? '&leftarrow;登录' : '注册&rightarrow;' }}</el-link
        >
      </el-form>
    </el-col>
  </el-row>
</template>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
.bg {
  background: url('@/assets/bg.jpg') no-repeat 60% center / auto;
  height: 100vh;
  position: relative;
}
.bg img {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
form {
  width: 100%;
}
form > div {
  margin: 20px 0px;
}
</style>
